<template>
  <div class="app-views">
    <div
      class="item-box"
      v-loading="loading"
      style="border-right: 1px dashed #e6e6e6"
    >
      <div class="sub-title">
        <i
          class="iconfont icon-list"
          style="margin-right: 5px; font-weight: bold"
        ></i>
        <div class="Title">设备列表</div>
      </div>
      <div class="device-list">
        <DCard
          :dName="item.name"
          :isCheck="isCheckCard == item.value"
          @click.native="handlerisCheck(item)"
          v-for="(item, index) in recumentList"
          :key="index"
          :status="deviceList[index]"
          :use="0"
        />
      </div>
    </div>
    <div class="device-views">
      <div class="sub-title">
        <i class="iconfont icon-shebeituceng-" style="margin-right: 5px"></i>
        <div class="Title">设备图</div>
      </div>
      <div class="device-img">
        <div class="device-img-rec">
          <RecumentImg :index="isCheckCard + 1" />
          <div class="deviceStatus">
            <div class="title">
              设备状态
              <i
                class="el-icon-edit"
                @click="openParamSet"
                v-hasPermi="['assembly:device:edit']"
              ></i>
            </div>
            <div class="list" v-loading="showDeviceStatus.length == 0">
              <div class="item" v-for="item in showDeviceStatus" :key="item.id">
                <div class="descript">
                  <span class="note"> {{ item.note }}</span>
                  <span class="value">
                    {{ item.message }}
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="signal">
          <div class="sub-title">
            <div class="Title">信号列表</div>
          </div>
          <div class="signal-table">
            <el-table :data="tableData" style="width: 100%" height="100%">
              <el-table-column prop="id" label="ID" width="80" />
              <el-table-column
                prop="cmd"
                label="指令内容"
                width="200"
                show-overflow-tooltip
              />
              <el-table-column
                prop="cmdType"
                label="命今类型"
                show-overflow-tooltip
              />
              <el-table-column prop="createTime" label="时间" width="150">
                <template slot-scope="scope">
                  {{ scope.row.createTime.substr(10) }}
                </template>
              </el-table-column>
              <el-table-column label="完成状态" width="100">
                <template slot-scope="scope">
                  <el-tag :type="scope.row.state | tagType">{{
                    scope.row.state | status
                  }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                  <el-button
                    icon="el-icon-close"
                    type="text"
                    v-if="!scope.row.state"
                    v-hasPermi="['assembly:device:edit']"
                    @click="handlerCmd('fallbackAction', scope.row)"
                    >取消</el-button
                  >
                  <el-button
                    icon="el-icon-check"
                    type="text"
                    v-if="!scope.row.state"
                    v-hasPermi="['assembly:device:edit']"
                    @click="handlerCmd('executionCompleted', scope.row)"
                    >完成</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <div class="item-box">
      <LogView :jobGroup="1" :isShow="isShow" />
    </div>
    <el-drawer title="设备参数" :visible.sync="drawer" :direction="direction">
      <div
        class="params"
        v-if="deviceParams != null"
        v-loading="loadDeviceParam"
      >
        <div class="param_item_W">
          <div class="paramsName">是否使用</div>
          <div class="paramView">
            <el-radio-group v-model="deviceParams.object.use">
              <el-radio :label="true">开启</el-radio>
              <el-radio :label="false">关闭</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">直行模式</div>
          <div class="paramView">
            <el-radio-group v-model="deviceParams.object.directModel">
              <el-radio :label="true">开启</el-radio>
              <el-radio :label="false">关闭</el-radio>
            </el-radio-group>
          </div>
          <div></div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">分流选项</div>
          <div class="paramView">
            <el-checkbox-group v-model="deviceParams.object.canUseEdgeId">
              <el-checkbox :label="1" :disabled="isDisable">一线</el-checkbox>
              <el-checkbox :label="2" :disabled="isDisable">二线</el-checkbox>
              <el-checkbox :label="3" :disabled="isDisable">三线</el-checkbox>
              <el-checkbox :label="4" :disabled="isDisable">四线</el-checkbox>
              <el-checkbox :label="5" :disabled="!isDisable">五线</el-checkbox>
            </el-checkbox-group>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">玻璃间隔</div>
          <div class="paramView">
            <span v-show="isShowSpaceValue" @click.stop="handlerSpace">{{
              deviceParams.object.space
            }}</span>
            <el-input-number
              v-show="!isShowSpaceValue"
              size="mini"
              ref="space"
              v-model="deviceParams.object.space"
              @blur="handlerSpace"
            ></el-input-number>
          </div>
        </div>
        <div class="footer">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="handlerEditDevice">确定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import DCard from "@/views/components/production/dCard.vue";
import LogView from "@/views/components/production/logViews.vue";
import RecumentImg from "@/views/production/device/components/RecumentImg.vue";
import { recumentList } from "../Config/label";
import mixin from "../mixin/index";
import { deviceRecumentDB } from "@/config/db";
import { mapState } from "vuex";
import { deviceEditParam } from "@/api/assembly/device";
export default {
  components: {
    DCard,
    LogView,
    RecumentImg,
  },
  props: {},
  mixins: [mixin],
  data() {
    return {
      drawer: false,
      direction: "rtl",
      recumentList,
      isCheckCard: 0,
      data: [],
      jobGroup: 0,
      loading: false,
      laodStatus: false,
      isShow: false,
      tableData: [],
      device: null,
      timer: null,
      deviceParams: null,
      isShowSpaceValue: true,
      loadDeviceParam: true,
    };
  },
  methods: {
    // index 选中的card
    handlerisCheck(item) {
      this.isCheckCard = item.value;
      this.device = item;
      this.getDeviceSignal();
    },
    async openParamSet() {
      this.drawer = true;
      this.initData();
    },
    handlerSpace() {
      this.isShowSpaceValue = !this.isShowSpaceValue;
      this.$nextTick(() => {
        !this.isShowSpaceValue && this.$refs.space.focus();
      });
    },
    handlerEditDevice() {
      this.$prompt("请输入密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^5563$/,
        inputErrorMessage: "密码错误",
        inputType: "password",
      })
        .then(({ value }) => {
          deviceEditParam(this.deviceParams).then((res) => {
            if (res.code == 200) {
              this.$message.success("修改设备状态成功");
              this.drawer = false;
              this.initData();
            } else {
              this.$message.error("修改设备状态失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    async initData() {
      this.loadDeviceParam = true;
      let paramList = this.recumentList.map((item) => {
        return {
          name: item.deviceType,
          id: item.id,
        };
      });
      await this.$store.dispatch("getDeviceMap", paramList);
      this.deviceParams =
        this.deviceParamsList.length == 0
          ? []
          : this.deviceParamsList[this.isCheckCard];
      this.loadDeviceParam = false;
    },
  },
  watch: {},
  created() {
    this.isCheckCard = this.$route.params.check || 0;
    this.device = recumentList[this.isCheckCard];
    this.initData();
    this.getDeviceSignal();
    this.getSignal(deviceRecumentDB);
  },
  destroyed() {},
  computed: {
    ...mapState({
      deviceParamsList: (state) => state.deviceStatus.deviceParams,
    }),
    showDeviceStatus() {
      if (this.deviceList.length == 0) return [];
      let blockAddr = [0, 4, 8, 20, 32, 72];
      let deviceBlock = deviceRecumentDB[this.isCheckCard].block;
      return this.deviceList[this.isCheckCard].plcSignals.filter(
        (item) => item.block == deviceBlock && blockAddr.includes(item.addr)
      );
    },
    isDisable() {
      return this.isCheckCard == 2 ? true : false;
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../style/index.less");
</style>
